package cn.yzhg.lib.tools

import android.util.Log

/**
 * 类 名: LogTools
 * 作 者: yzhg
 * 创 建: 2018/11/8 0008
 * 版 本: 1.0
 * 历 史: (版本) 作者 时间 注释
 * 描 述: 日志工具类,用来打印Log日志
 */
class LogTools {

    companion object {

        private var yHttp: LogTools? = null
            /**
             * field 解释https://www.jishux.com/p/228468f108ac1bf4
             * 懒汉式加载
             */
            get() {
                if (field == null) {
                    field = LogTools()
                }
                return field
            }

        //线程安全的懒汉式
        @Synchronized
        fun get(): LogTools {
            return yHttp!!
        }

        /*双重锁单利模式*/
        val instance: LogTools by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { LogTools() }
    }


    var DEBUG: Boolean = true


    /***
     * 获取当前类名
     */
    fun getClassName(): String {
        val stackTraceElement: StackTraceElement = Exception().stackTrace[2]
        var className: String = stackTraceElement.className
        val lastIndexOf: Int = className.lastIndexOf(".")
        className = className.substring(lastIndexOf + 1, className.length)
        return "----------${className}----------"
    }

    fun v(msg: String) {
        if (DEBUG) {
            Log.v(getClassName(), msg)
        }
    }

    fun v(name: String, msg: String) {
        if (DEBUG) {
            Log.v(name, msg)
        }
    }

    fun w(msg: String) {
        if (DEBUG) {
            Log.w(getClassName(), msg)
        }
    }


    fun w(name: String, msg: String) {
        if (DEBUG) {
            Log.v(name, msg)
        }
    }

    fun d(msg: String) {
        if (DEBUG) {
            Log.d(getClassName(), msg)
        }
    }


    fun d(name: String, msg: String) {
        if (DEBUG) {
            Log.v(name, msg)
        }
    }

    fun e(msg: String) {
        if (DEBUG) {
            Log.e(getClassName(), msg)
        }
    }


    fun e(name: String, msg: String) {
        if (DEBUG) {
            Log.v(name, msg)
        }
    }

    fun i(msg: String) {
        if (DEBUG) {
            Log.i(getClassName(), msg)
        }
    }


    fun i(name: String, msg: String) {
        if (DEBUG) {
            Log.v(name, msg)
        }
    }
}














